<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Agent;
use App\Models\Cities;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AgentController extends AdminController
{
    /**
     * Author:Zhongyuan Lu
     * 经纪人管理列表展示
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Agent(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('nick_name');        //经纪人名称

            $grid->column('phone')->display(function ($value) {
                return substr_replace($value,'******',3,6);
            });

            $grid->column('avatar')->image(config('setting.uploadUrl'),100,100);
            $grid->column('id_number')->display(function ($value) {
                return substr_replace($value,'************',3,11);
            });

            $grid->column('company');           //公司
            $grid->column('new_house_num');     //新房数量
            $grid->column('second_hand_house_num');     //二手房数量
            $grid->column('rent_house_num');    //租房数量
            $grid->status()->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->setActionClass(Grid\Displayers\Actions::class);
        });
    }

    /**
     * Author:Zhongyuan Lu
     * 经纪人详情
     * @param $id
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Agent(), function (Show $show) {
            $show->field('id');
            $show->field('nick_name');
            $show->field('phone');
            $show->field('avatar');
            $show->field('id_number')->value(function ($value) {
                return substr_replace($value,'*************',3);
            });
            $show->field('company');
            $show->field('new_house_num');
            $show->field('second_hand_house_num');
            $show->field('rent_house_num');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');

        });
    }

    /**
     * Author:Zhongyuan Lu
     * 添加经济人
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Agent(), function (Form $form) {
            $form->display('id');
            $form->text('nick_name');
            $form->text('phone');
            $form->image('avatar');
            $form->text('id_number');
            $form->text('company');
            $form->number('new_house_num');
            $form->number('second_hand_house_num');
            $form->number('rent_house_num');
            // 省份
            $form->select('province')->options(
                Cities::where('pid', 0)->get()->pluck('name', 'id')
            )->rules('required')->load('city', '/city');
            // 城市
            $form->select('city')->load('region', '/city');
            // 区域
            $form->select('region');
            $form->radio('status','状态')->options([0=>'禁用',1=>'开启'])->default(0);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
